//
//  CZSettingsViewController.m
//  nsj_iOS
//
//  Created by apple on 2017/1/24.
//  Copyright © 2017年 GCZ. All rights reserved.
//

#import "CZSettingsViewController.h"
//#import "CZMineCell.h"
#import "CZSettingCell.h"
#import "CZAboutUsViewController.h"
static NSString *const setting_cell = @"setting_cell";
@interface CZSettingsViewController () <UITableViewDelegate, UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *settingTableView;
@property (nonatomic, copy) NSArray *titleArray;

@end

@implementation CZSettingsViewController

- (NSArray *__nonnull)titleArray {
    if (!_titleArray) {
        _titleArray = @[@"联系客服", @"清除缓存", @"关于我们", @"版本"];
    }
    return _titleArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"设置";
    _settingTableView.rowHeight = 50;
    _settingTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [_settingTableView registerNib:[UINib nibWithNibName:@"CZSettingCell" bundle:nil] forCellReuseIdentifier:setting_cell];
    // Do any additional setup after loading the view from its nib.
}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden = YES;
    self.navigationController.navigationBar.alpha = 1;
    
    [(UIView*)[self.navigationController.navigationBar.subviews objectAtIndex:0] setAlpha:1.f];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 4;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    CZSettingCell *cell = [tableView dequeueReusableCellWithIdentifier:setting_cell];
    cell.titleLabel.text = self.titleArray[indexPath.row];
    cell.titleLabel.left = cell.contentView.left + 20;
    cell.accessoryType = UITableViewCellAccessoryNone;
    YYImageCache *cache = [YYWebImageManager sharedManager].cache;
    switch (indexPath.row) {
        case 0:
            cell.contentLabel.text = @"0574-55001901";
            break;
        case 1: {
            // 获取缓存大小
            
            if (cache.diskCache.totalCost < 1024 * 1024) {
                cell.contentLabel.text = [NSString stringWithFormat:@"%dKB", cache.diskCache.totalCost / 1024];
            } else {
                cell.contentLabel.text = [NSString stringWithFormat:@"%.2fMB", (CGFloat)cache.diskCache.totalCost / 1024 / 1024];
            }
        }
            break;
        case 2:
            cell.contentLabel.text = @"";
            cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
            break;
        case 3: {
            NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
            // app版本
            NSString *app_Version = [infoDictionary objectForKey:@"CFBundleShortVersionString"];
            cell.contentLabel.text = app_Version;
        }
            break;
            
        default:
            break;
    }

    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.row == 0) {
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"tel:0574-55001901"]];
        return;
    }
    
    if (indexPath.row == 1) {
        // 清除缓存
        UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"提示" message:@"是否清除缓存?" preferredStyle:(UIAlertControllerStyleAlert)];
        YYImageCache *cache = [YYWebImageManager sharedManager].cache;
        UIAlertAction *confirmAction = [UIAlertAction actionWithTitle:@"确认" style:(UIAlertActionStyleDestructive) handler:^(UIAlertAction * _Nonnull action) {
            [cache.diskCache removeAllObjectsWithProgressBlock:^(int removedCount, int totalCount) {
                
            } endBlock:^(BOOL error) {
                if (!error) {
                    [SVProgressHUD showSuccessWithStatus:@"清除缓存成功"];
                    dispatch_async(dispatch_get_main_queue(), ^{
                        CZSettingCell *cell = [tableView cellForRowAtIndexPath:indexPath];
                        cell.contentLabel.text = @"0KB";
                    });
                }

            }];
        }];
        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:(UIAlertActionStyleCancel) handler:^(UIAlertAction * _Nonnull action) {
            [alertVC dismissViewControllerAnimated:YES completion:nil];
        }];
        [alertVC addAction:cancelAction];
        [alertVC addAction:confirmAction];
        [self presentViewController:alertVC animated:YES completion:nil];
    }
    
    if (indexPath.row == 2) {
        CZAboutUsViewController *aboutUS = [[CZAboutUsViewController alloc] init];
        [self.navigationController pushViewController:aboutUS animated:YES];
    }
    
    
        
    
}
@end
